<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: mh<mahuan@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;

class ShopGoodsActivityModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        //sale_goods_id验证
        array('goods_activity_id', '/^[1-9]\d*/', '传入的goods_activity_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_UPDATE),
        array('goods_activity_id', '/^[1-9]\d*/', '插入的goods_activity_id不正确', self::EXISTS_VALIDATE, 'regex', self::MODEL_INSERT),
        //activity_id
        array('activity_id', '/^[1-9]\d*/', '传入的activity_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //merchant_id
        array('merchant_id', '/^[1-9]\d*/', '传入的merchant_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //shop_id
        array('shop_id', '/^[1-9]\d*/', '传入的shop_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //goods_id
        array('goods_id', '/^[1-9]\d*/', '传入的goods_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        // //promotion_id
        // array('promotion_id', '/^[1-9]\d*/', '传入的promotion_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //pic_id
        array('pic_id', '/^[1-9]\d*/', '传入的pic_id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //goods_content
        array('goods_content', 'require', '介绍内容必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //名称20字以内
        array('goods_content', '1,50', '介绍内容必须在50字以内', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        //member_id
        array('member_id', '/^[1-9]\d*/', '传入的创建人id不正确', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH)
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '0', self::MODEL_INSERT)
    );

    public function getPk(){
        return "goods_activity_id";
    }

    /**
     * 全局新增
     * @return boolean fasle 失败 ， int  成功 返回完整的数据
     * @author mahuan <mahuan1768@126.com>
     */
    public function update(){
        /* 获取数据对象 */
        $data = $this->create($_POST);
        if(empty($data)){
            $this->error =$this->getError();
            return false;
        }
        $goods_id = (int)I("goods_id");
        if($goods_id == 0){
            $this->error = "商品id不正确!";
            return false;
        }
        /* 添加或新增活动配置 */
        if(empty($data[$this->getPk()])){ //新增数据
            //查找新增数据是否存在
            $goods_exit_map["status"] = array("neq",-1);
            $goods_exit_map["goods_id"] = $goods_id;
            $goods_exit = $this->field('goods_activity_id')->where($goods_exit_map)->find();
            if(false === $goods_exit){
                $this->error = '查找商品数据错误';
                return false;
            }
            if(!empty($goods_exit)){
                $this->error = '你要添加的商品已经存在';
                return false;
            }
            $id = $this->add(); //添加活动配置
            if(!$id){
                $this->error = '新增出错！';
                return false;
            }
        } else { //更新数据
            //验证是否修改
            if(empty($_SESSION['shop_good_activity_goods_id'])){
                $this->error = '你要修改的界面已过期，请重新登录!';
            }
            //若提交的内容与上次不相等，则验证
            if($_SESSION['shop_good_activity_goods_id'] != $goods_id){
                $goods_exit_map["status"] = array("neq",-1);
                $goods_exit_map["goods_id"] = $goods_id;
                $goods_exit = $this->field('goods_activity_id')->where($goods_exit_map)->find();
                if(false === $goods_exit){
                    $this->error = '查找商品数据错误';
                    return false;
                }
                if(!empty($goods_exit)){
                    $this->error = '你要添加的商品已经存在';
                    return false;
                }
            }
            $id = $this->save(); //更新活动配置
            if(false === $id){
                $this->error = '更新出错！';
                return false;
            }
        }
        return $id;
    }
}